package cn.hpclub.server.controller.admin;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.SplashImage;
import cn.hpclub.server.service.SplashImageService;
import cn.hpclub.server.util.ResultCode;
import cn.hpclub.server.validator.admin.SplashImageValidator;

public class SplashImageController extends BaseAdminController<SplashImage>{
    private static final Logger logger    = LoggerFactory.getLogger(SplashImageController.class);
    private static final String HTML_LIST = "/admin/splash_image_list.tpl";
    private static final String URL_LIST  = "/splashImage/list";
    private static final String HTML_ADD  = "/admin/splash_image_input.tpl";
    private static final String SID       = "sid";

    /**
     * @Title: list
     * @Description:主题列表界面，包含搜索和分页
     */
    public void list(){
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        findByPageForSearch("select * ", SplashImageService.service.getSqlExceptSelect(searchInfo));
        setAttrs(searchInfo);
        render(HTML_LIST);
    }

    @Override
    public void delete(){
        String[] ids = getParaValues(Constant.IDS);
        ResultCode result = SplashImageService.service.delete(ids);

        if(result == ResultCode.SUCCESS){
            ajaxJsonSuccessMessage(Constant.Msg.DEL_DONE);
        } else if(result == ResultCode.PARAM_ERROR){
            ajaxJsonErrorMessage(Constant.Msg.DEL_FAILED);
        } else if(result == ResultCode.FAILED){
            ajaxJsonErrorMessage(Constant.Msg.DEL_FAILED);
        }
    }

    /**
     * @Title: save
     * @Description:新增保存
     */
    @Before({ Tx.class, SplashImageValidator.class })
    public void save(){
        saveSplashImage();
    }

    /**
     * @Title: update
     * @Description:编辑保存
     */
    @Before({ Tx.class, SplashImageValidator.class })
    public void update(){
        saveSplashImage();
    }

    private void saveSplashImage(){
        SplashImage splashImage = getModel(SplashImage.class);
        splashImage.set("title", getPara("splashImage.title"));
        splashImage.set("enabled", getPara("splashImage.enabled"));
        splashImage.set("link_url", getPara("splashImage.link_url"));
        splashImage.set("img_url", getPara("splashImage.img_url"));
        splashImage.set("img_size", getPara("splashImage.img_size"));
        splashImage.set("remark", getPara("splashImage.remark"));
        splashImage.set("create_time", new Date());

        String id = getPara("splashImage.id");
        if(StrKit.notBlank(id)){
            // 修改主题时，不修改主题的type
            splashImage.set("id", id);
            splashImage.update();
        } else{
            // 新增一条，默认为0.如果非0，需要手动去修改themes对应条目的type字段。
            splashImage.save();
        }
        redirect(URL_LIST);
    }

    /**
     * 新增
     */
    public void add(){
        page();
    }

    /**
     * 编辑
     */
    public void edit(){
        page();
    }

    /**
     * @Title: page
     * @Description:跳到新增编辑页面
     */
    private void page(){
        String sid = getSession().getId();
        String id = getPara("id");
        if(StrKit.notBlank(id)){
            SplashImage splashImage = SplashImage.dao.findById(id);
            setAttr("splashImage", splashImage);
            setAttr("enabled", splashImage.getInt("enabled"));
        } else{
            setAttr("enabled", 0);
        }
        setAttr(SID, sid);
        render(HTML_ADD);
    }
}
